package org.makcro.sms.services.chat.base.business;

import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.makcro.sms.services.chat.base.dao.QuestionDao;
import org.makcro.sms.services.chat.base.dao.QuestionResponsibilityDao;
import org.makcro.sms.services.chat.base.dto.HistoryItem;
import org.makcro.sms.services.chat.base.service.ChatService;
import org.makcro.sms.services.chat.model.Agent;
import org.makcro.sms.services.chat.model.Question;
import org.makcro.sms.services.chat.model.QuestionResponsibility;

public class MessageService {

	private Logger						logger	= Logger.getLogger( MessageService.class );
	private QuestionDao					questionDAO;
	private QuestionResponsibilityDao	questionResponsabilityDao;
	private ChatService					chatService;

	public List<Question> findNewMessages() {
		// find new messages
		List<Question> findAnswered = questionDAO.findUnassigned();

		return findAnswered;
	}

	public List<HistoryItem> findHistory(String phoneNumber) throws SQLException {
		List<HistoryItem> history = questionDAO.findHistory( phoneNumber );

		return history;
	}

	public Boolean checkAndReserveQuestion(Integer questionId, Agent currentAgent) {
		// check if this question is already associated with an agent
		QuestionResponsibility testQuestionResponsibility = questionResponsabilityDao.findByQuestionId( questionId );
		if (testQuestionResponsibility != null) {
			return false;
		}

		// find the question by it's id
		Question question = questionDAO.find( questionId );

		// reserve the question
		QuestionResponsibility questionResponsibility = new QuestionResponsibility();
		questionResponsibility.setActive( true );
		questionResponsibility.setAgent( currentAgent );
		questionResponsibility.setQuestion( question );

		questionResponsabilityDao.create( questionResponsibility );

		return true;
	}

	public Long sendMessage(Long referenceId, String messageText) {
		Question question = questionDAO.findByCoreId( referenceId );
		Long sendingResult = chatService.sendResponseMessage( referenceId, messageText, question );
		return sendingResult;
	}

	public List<HistoryItem> findByAgent(Agent currentAgent) {

		List<HistoryItem> historyByAgent = null;

		try {
			historyByAgent = questionDAO.findByAgent( currentAgent );
		}
		catch (SQLException e) {

			e.printStackTrace();
		}

		return historyByAgent;
	}

	public List<HistoryItem> findByAgent(Agent currentAgent, int[] pagingParams) {
		List<HistoryItem> historyByAgent = null;
		try {
			historyByAgent = this.questionDAO.findByAgent( currentAgent, pagingParams );
		}
		catch (SQLException e) {
			logger.fatal( "Error while loading agents history", e );
		}
		return historyByAgent;
	}

	public int findCountByAgent(Agent agent) {
		int result = 0;

		try {
			result = this.questionDAO.findCountByAgent( agent );
		}
		catch (SQLException e) {
			logger.fatal( "Error while finding count by agent", e );
		}

		return result;
	}

	public void cancelQuestionReservation(Integer questionId, Agent agent) {
		questionResponsabilityDao.deleteByQuestionIdAndAgent( questionId, agent );
	}

	public void setQuestionDAO(QuestionDao questionDAO) {
		this.questionDAO = questionDAO;
	}

	public void setQuestionResponsabilityDao(QuestionResponsibilityDao questionResponsabilityDao) {
		this.questionResponsabilityDao = questionResponsabilityDao;
	}

	public void setChatService(ChatService chatService) {
		this.chatService = chatService;
	}

	public List<HistoryItem> getAllHistory() {
		List<HistoryItem> allAnswered = null;
		try {
			allAnswered = questionDAO.findAnswered();
		}
		catch (SQLException e) {

			logger.fatal( "Unexpected error while finding all history", e );
		}
		return allAnswered;
	}

	public List<HistoryItem> getAllHistory(int[] pagingConfig) {
		try {
			return this.questionDAO.findAnswered( pagingConfig );
		}
		catch (SQLException e) {
			logger.fatal( "Error while fetching all history", e );

		}
		return null;
	}

	public int getAllHistoryCount() {
		try {
			return this.questionDAO.findAllAnsweredCount();
		}
		catch (SQLException e) {
			logger.fatal( "Error while fetching all history count", e );
		}
		return 0;
	}

}
